package me.numark.view;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import me.numark.R;
import me.numark.fragments.FriendsFragment;
import me.numark.model.User;
import me.numark.utils.LogUtils;

/**
 * Created by ytian on 15/8/1.
 */
public class UsersListAdapter extends UltimateViewAdapter<UserListViewHolder> {
    List<User> dataList;
    Object mParent;
    int mUserId;
    int type;
    public static final int TYPE_FROM_REGESITER = 1;
    public static final int TYPE_NORMAL = 2;
    FriendsFragment mFragment;

    public UsersListAdapter(List<User> dataList, Object parent,int userId ,int type) {
        this.dataList = dataList;
        mParent = parent;
        mUserId = userId;
        this.type = type;
        mFragment = (FriendsFragment)mParent;
    }

    @Override
    public UserListViewHolder getViewHolder(View view) {
        return new UserListViewHolder(view, mParent, UserListViewHolder.TYPE_NONE,0);
    }

    @Override
    public UserListViewHolder onCreateViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.friend_list_item, parent, false);
        int mtype = UserListViewHolder.TYPE_NORMAL;
        if (type == TYPE_FROM_REGESITER){
            mtype = UserListViewHolder.TYPE_FRIST_ADD;
        }
        return new UserListViewHolder(v, mParent, mtype,mUserId);
    }

    @Override
    public int getAdapterItemCount() {
        return dataList.size();
    }

    @Override
    public long generateHeaderId(int i) {
        return 0;
    }

    @Override
    public void onBindViewHolder(UserListViewHolder holder, int position) {
        if (position < dataList.size()) {
            User user = dataList.get(position);
            holder.bindData(user);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    }

    public void append(User item) {
        insert(dataList, item, dataList.size());
    }

    public void append(User[] items) {
        int pos = dataList.size();
        for (User item : items) {
            if (!dataList.contains(item)) {
                dataList.add(item);
            }
            if (type == TYPE_FROM_REGESITER){

                if (mFragment.mCheckedUsers.contains(item.uid)) {
                   // mFragment.mCheckedUsers.remove(item.uid);
                } else {
                    mFragment.mCheckedUsers.add(item.uid);
                    LogUtils.d("added checked: " + item.uid);
                }
            }
        }
        notifyItemRangeInserted(pos, items.length);

    }

    public void remove(int position) {
        remove(dataList, position);
    }

    public void clear() {
        clear(dataList);
    }

    @Override
    public long getItemId(int position) {
        return dataList.get(position).uid;
    }
}
